package cn.com.cfae.iras.doc.analyze.components.integrity;

import cn.com.cfae.iras.doc.analyze.beans.web.Detail;
import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.handUtil.HandingData;
import cn.com.cfae.iras.doc.analyze.handUtil.SearchKeywordUtil;
import cn.com.cfae.iras.doc.analyze.parse.model.*;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 架构完整性
 *
 * 是否涉及重大资产重组相关描述
 *
 * 预评/发行阶段
 */
@Component("I1018")
@Scope("prototype")
public class I1018 extends BaseComponent {
    private static Logger log = LoggerFactory.getLogger(I1018.class);
    @Autowired
    private HandingData handingData;
    @Autowired
    private SearchKeywordUtil searchKeywordUtil;
    @Override
    public List<ComponentResult> handlerM() {
        log.info("获取I1018组件M");
        List<ComponentResult> componentResults = checkRulesI1018(getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBM() {
        log.info("获取I1018组件BM");
        List<ComponentResult> componentResults = checkRulesI1018(getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerDM() {
        return null;
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        return null;
    }

    private List<ComponentResult> checkRulesI1018(String catalogId,String chapterId,String ruleName){
        DocumentModel extractData = super.getExtractData();
        List<ComponentResult> componentResults = new ArrayList<>();
        ComponentResult componentResult = null;
        ExtractItem extractItem = null;
        int pageNum = 0;
        List<ComponentResult.TextPosition> contentAndPositionList =new ArrayList<>();
        ComponentResult.TextPosition contentAndPosition =null;
        boolean flag = false;
        boolean bsjFlag =false;
        Double x = 0.0;
        Double y = 0.0;
        String noRegex = ".*(不存在|不涉及|不构成).*重大资产重组.*";
        //String regex = ".*重大资产重组.*";
        String regex = "重大资产重组";

        try {
            if (extractData!=null){
                List<ChapterModel> chapterModels = extractData.getChapterModels();
                if (CollectionUtils.isNotEmpty(chapterModels)){

                    for (ChapterModel chapterModel : chapterModels){
                        List<PageModel> pageModelList = chapterModel.getPageModelList();
                        if (CollectionUtils.isNotEmpty(pageModelList)){
                            for (PageModel pageModel :pageModelList){
                                Map<String, Object> containsKeyWord = handingData.isContainsKeyWordOfRegex(pageModel.getJsonTextData(), noRegex);
                                bsjFlag = (boolean)containsKeyWord.get("flag");
                                if (bsjFlag){
                                    break;
                                }
                            }
                        }
                        if (bsjFlag)
                            break;
                    }
                    if (!bsjFlag){
                        for (ChapterModel chapterModel : chapterModels){
                            List<PageModel> pageModelList = chapterModel.getPageModelList();
                            if (CollectionUtils.isNotEmpty(pageModelList)){
                                for (PageModel pageModel :pageModelList){
                                    //Map<String, Object> containsKeyWord = handingData.isContainsKeyWordOfRegex(pageModel.getJsonTextData(), regex);
                                  //  Map<String, Object> containsKeyWordOfRegex2Lines = handingData.isContainsKeyWord2Lines(pageModel.getJsonTextData(), regex);
                                    Map<String, Object> containsKeyWordOfRegex2Lines = searchKeywordUtil.isContainsKeywords2LiensOfSynonyms(pageModel.getJsonTextData(), regex,pageModel);
                                    flag = (boolean)containsKeyWordOfRegex2Lines.get("flag");
                                    if (flag){
                                        pageNum = pageModel.getPageNumber();
                                        contentAndPositionList = (List<ComponentResult.TextPosition>)containsKeyWordOfRegex2Lines.get("position");
                                        break;
                                    }
                                }
                                pageNum = pageNum!=0?pageNum:1;
                            }
                            if (flag)
                                break;
                        }
                    }
                }
            }
            if (flag){
                componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"关键词检查-是否涉及重大资产重组相关描述","可能涉及重大资产重组，需要按照M.4披露",true,getRegulationId(),pageNum,contentAndPositionList,"03");
                componentResults.add(componentResult);
            }else {
                componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"关键词检查-是否涉及重大资产重组相关描述","未披露",true,getRegulationId(),1,contentAndPositionList,"02");
                componentResults.add(componentResult);
            }
        }catch (Exception e){
            componentResults.clear();
            componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"关键词检查-是否涉及重大资产重组相关描述","未识别,建议人工核对",true,getRegulationId(),1,contentAndPositionList,"03");
            componentResults.add(componentResult);
            log.error(e.getMessage(),e);
        }


        return componentResults;
    }

}
